/*
	File:		ObjectManager.cpp
	Author:		Ben Feldmann
	Purpose:	Contain/Manage all game entities	
*/

#include "ObjectManager.h"

typedef vector<IEntity*>::iterator iterator;


ObjectManager* ObjectManager::GetInstance() 
{
	static ObjectManager instance;
	return &instance;
}

//void ObjectManager::DeleteInstance() 
//{
//	throw "Not yet implemented";
//}

void ObjectManager::AddObject(IEntity* object) 
{
	if(object == nullptr)
		return;

	objects.push_back(object);
}

void ObjectManager::RemoveObject(IEntity* object) 
{
	if(object == nullptr)
		return;

	iterator iter;

	for(iter = objects.begin(); iter != objects.end(); ++iter)
	{
		if((*iter) == object)
		{
			iter = objects.erase(iter);
			break;
		}
	}
}

void ObjectManager::UpdateAll(float elapsed) 
{
	iterator iter;

	for(iter = objects.begin(); iter != objects.end(); ++iter)
	{
		(*iter)->Update(elapsed);
	}
}

void ObjectManager::RenderAll() 
{
	iterator iter;

	for(iter = objects.begin(); iter != objects.end(); ++iter)
	{
		(*iter)->Render();
	}
}


void ObjectManager::CheckCollisions() 
{
	iterator iter;
	iterator iter2;

	for(iter = objects.begin(); iter != objects.end(); ++iter)
	{
		if((*iter)->GetType() != 4)
		{
			for(iter2 = objects.begin(); iter2 != objects.end(); ++iter2)
			{
				if((*iter)->GetType() != (*iter2)->GetType())
				{
					if((*iter)->CheckCollision((*iter2)))
					{
						break;
					}
				}
			}
		}
	}
}

void ObjectManager::RemoveAllObjects() 
{
	objects.clear();
}


